using System;
using NUnit.Framework;
using dnA.Math;

namespace dnA.dnAtests.Math {
    [TestFixture]
    public class ComplexFloatTest {
       
        [Test]
        public void EqualsTest() {
            ComplexFloat cd1 = new ComplexFloat(-1.1f, 2.2f);
            ComplexFloat cd2 = new ComplexFloat(-1.1f, 2.2f);
            ComplexFloat cd3 = new ComplexFloat(-1, 2);
            Assert.IsTrue(cd1 == cd2);
            Assert.IsTrue(cd1.Equals(cd2));
        }

        [Test]
        public void NaNTest() {
            ComplexFloat cd = new ComplexFloat(Single.NaN, 1.1f);
            Assert.IsTrue(cd.IsNaN());
            cd = new ComplexFloat(1.1f, Single.NaN);
            Assert.IsTrue(cd.IsNaN());
            cd = new ComplexFloat(1.1f, 2.2f);
            Assert.IsFalse(cd.IsNaN());
        }

        [Test]
        public void InfinityTest() {
            ComplexFloat cd = new ComplexFloat(Single.NegativeInfinity, 1.1f);
            Assert.IsTrue(cd.IsInfinity());
            cd = new ComplexFloat(1.1f, Single.NegativeInfinity);
            Assert.IsTrue(cd.IsInfinity());
            cd = new ComplexFloat(Single.PositiveInfinity, 1.1f);
            Assert.IsTrue(cd.IsInfinity());
            cd = new ComplexFloat(1.1f, Single.PositiveInfinity);
            Assert.IsTrue(cd.IsInfinity());
            cd = new ComplexFloat(1.1f, 2.2f);
            Assert.IsFalse(cd.IsInfinity());
        }

        [Test]
        public void HashTest() {
            ComplexFloat cd = new ComplexFloat(1.1f, 2.2f);
            Assert.AreEqual(cd.GetHashCode(), 11);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void NullString() {
            string s = null;
            ComplexFloat cd = new ComplexFloat(s);
        }

        [Test]
        [ExpectedException(typeof(FormatException))]
        public void FormatExceptionTest1() {
            string s = "";
            ComplexFloat cd = new ComplexFloat(s);
        }

        [Test]
        [ExpectedException(typeof(FormatException))]
        public void FormatExceptionTest2() {
            string s = "+";
            ComplexFloat cd = new ComplexFloat(s);
        }

        [Test]
        [ExpectedException(typeof(FormatException))]
        public void FormatExceptionTest3() {
            string s = "1i+2";
            ComplexFloat cd = new ComplexFloat(s);
        }

        [Test]
        public void ParseTest() {
            string s = "1";
            ComplexFloat cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, 0);

            s = "i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 0);
            Assert.AreEqual(cd.Imag, 1);

            s = "2i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 0);
            Assert.AreEqual(cd.Imag, 2);

            s = "1 + 2i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, 2);

            s = "1+2i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, 2);

            s = "1 - 2i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, -2);

            s = "1-2i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, -2);

            s = "1+-2i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, -2);

            s = "1 - 2i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, -2);

            s = "1,2";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, 2);

            s = "1 , 2 ";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, 2);

            s = "1,2i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, 2);

            s = "-1, -2i";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, -1);
            Assert.AreEqual(cd.Imag, -2);

            s = "(+1,2i)";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 1);
            Assert.AreEqual(cd.Imag, 2);

            s = "(-1 , -2)";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, -1);
            Assert.AreEqual(cd.Imag, -2);

            s = "(-1 , -2i)";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, -1);
            Assert.AreEqual(cd.Imag, -2);


            s = "(+1e1 , -2e-2i)";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, 10);
            Assert.AreEqual(cd.Imag, -.02);

            s = "(-1e1 + -2e2i)";
            cd = new ComplexFloat(s);
            Assert.AreEqual(cd.Real, -10);
            Assert.AreEqual(cd.Imag, -200);

        }
    }
}

